package com.xls.app;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import tv.danmaku.ijk.media.example.activities.VideoActivity;
import tv.danmaku.ijk.media.example.widget.media.AndroidMediaController;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class MainActivity extends AppCompatActivity {

    private RecyclerView rvVideolist;
    private VideoListAdpater mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        initView();


    }

    private void initView(){
        rvVideolist = (RecyclerView) findViewById(R.id.rv_videolist);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3,GridLayoutManager.VERTICAL, false);
        rvVideolist.setLayoutManager(gridLayoutManager);
        rvVideolist.addItemDecoration(new SpaceItemDecoration(3, 20, false));
        mAdapter = new VideoListAdpater(createTestData(),this);
        rvVideolist.setAdapter(mAdapter);


        // init player
        IjkMediaPlayer.loadLibrariesOnce(null);
        IjkMediaPlayer.native_profileBegin("libijkplayer.so");


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            List<VideoBean> list = createTestData();
            Intent intent = VideoActivity.newIntent(this,list.get(1).getUrl(),list.get(1).getTitle());
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private List<VideoBean> createTestData(){
        List<VideoBean> list = new ArrayList<>();
        VideoBean v1 = new VideoBean();
        v1.setTitle("王亭文同仁堂");
        v1.setUrl("http://qaimages.lyancafe.com/training/videos/1505381240699.mp4");
        list.add(v1);
        VideoBean v2 = new VideoBean();
        v2.setTitle("工会经费鸡飞狗叫和");
        v2.setUrl("http://qaimages.lyancafe.com/training/videos/1505376240236");
        list.add(v2);
        VideoBean v3 = new VideoBean();
        v3.setTitle("3热污染翁");
        v3.setUrl("http://qaimages.lyancafe.com/training/videos/1505382140099.flv");
        list.add(v3);
        VideoBean v4 = new VideoBean();
        v4.setTitle("羞羞的铁拳");
        v4.setUrl("http://qaimages.lyancafe.com/training/videos/1505895331859.flv");
        list.add(v4);
        VideoBean v5 = new VideoBean();
        v5.setTitle("维多利亚的秘密");
        v5.setUrl("http://qaimages.lyancafe.com/training/videos/1505894878565.mp4");
        list.add(v5);

        return list;
    }
}
